<?php
namespace App\Controllers;

class Travel extends BaseController
{
    public function index() 
    { 

         $places = new \App\Models\Places(); 

         $records = $places->findAll(); 

         $table = new \CodeIgniter\View\Table();

        
        $headings = $places->db->getFieldNames('places'); 
        $displayHeadings = array_slice($headings, 1, 2);
        $table->setHeading(array_map('ucfirst', $displayHeadings));

        foreach ($records as $record) {
            $nameLink = anchor("travel/showme/".$record['id'], $record['name']); 
            $table->addRow($nameLink, $record['description'],' <a href="/travel/delete/'.$record['id'].'">Delete</a>');
        }
        $template = [
            'table_open' => '<table cellpadding="10px">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
            ];
            $table->setTemplate($template);
            
            $fields = [
                'title' => 'Travel Destinations',
                'heading' => 'Travel Destinations',
                'footer' => 'Copyright Zhourenke',
                'create' => '<p><a href="/travel/create">Create a Destination</a></p>'
            ];

        $parser = \Config\Services::parser(); 

        return $parser->setData($fields)
                 ->render('templates\top') .
          $fields['create'] .
          $table->generate() .
          $parser->setData($fields)
                 ->render('templates\bottom');

    } 

    public function delete($id) 
    {
        $places = new \App\Models\Places();
        $places->delete($id);
        return redirect()->to('/travel');
    }
    
    public function edit($id = NULL) 
    { 
        $places = new \App\Models\Places();
        $record = $places->find($id);
        $fields = [
            'title' => 'Edit a Destination',
            'heading' => 'Edit a Destination',
            'footer' => 'Copyright Zhourenke'
        ];
        $parser = \Config\Services::parser();
        return $parser->setData($fields)
            ->render('templates\top') .
            $parser->setData($record)
                ->render('editplace') .
            $parser->setData($fields)
                ->render('templates\bottom');

    }

    public function handle() 
    { 

        $places = new \App\Models\Places(); 
        helper(['form', 'url']); 
        $id = $this->request->getPost('id'); 
        $record = $places->find($id); 
 
        if ($this->validate($places->validationRules)) { 
            if ($id == 0) { 
                $places->save([ 
                'name' => $this->request->getPost('name'), 
                'description' => $this->request->getPost('description'), 
                'link' => $this->request->getPost('link'), 
                'image' => $this->request->getPost('image'), 
                ]); 
            } else { 
                $places->save([ 
                'id' => $this->request->getPost('id'), 
                'name' => $this->request->getPost('name'), 
                'description' => $this->request->getPost('description'), 
                'link' => $this->request->getPost('link'), 
                'image' => $this->request->getPost('image'), 
                ]); 
            } 
        } 

        return redirect()->to('travel');
    }

    public function create($id = NULL) 
    { 
        $fields = [ 
            'title' => 'Create a Destination', 
            'heading' => 'Create a Destination', 
            'footer' => 'Copyright Zhourenke' 
        ]; 
 
        $record = [ 
            'id' => 0, 
            'name' => '', 
            'description' => '', 
            'link' => '', 
            'image' => '', 
        ]; 
 
        $parser = \Config\Services::parser(); 
        return $parser->setData($fields) 
            ->render('templates\top') . 
            $parser->setData($record) 
                ->render('editplace') . 
            $parser->setData($fields) 
                ->render('templates\bottom'); 
    }

    public function showme($id) 
    { 
        $places = new \App\Models\Places(); 
        // retrieve all the records 
        $record = $places->find($id); 

        $table = new \CodeIgniter\View\Table();
        $table->addRow('<p><a href="/travel/edit/'.$record['id'].'">Edit</a></p>');
        $table->addRow('Id',$record['id']);
        $table->addRow('Name',$record['name']);
        $table->addRow('Description',$record['description']);
        $table->addRow('Link',$record['link']);
        $table->addRow('Image','<img src=\\image\\' .$record['image'].'>');

        $template = [
            'table_open' => '<table cellpadding="20px" style="margin-top:30px; margin-bottom:30px;">',
            'cell_start' => '<td style="border: 1px solid #dddddd;">',
            'row_alt_start' => '<tr style="background-color:#dddddd">',
            ];
            $table->setTemplate($template);
            
            $fields = [
                'title' => ''.$record['name'].'',
                'heading' => ''.$record['name'].'',
                'footer' => 'Copyright Zhourenke'
            ];

        $parser = \Config\Services::parser(); 

        return $parser->setData($fields)
        ->render('templates\top') .
            $table->generate() .
        $parser->setData($fields)
        ->render('templates\bottom');

    }
}


